/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.services.ejb3.facade;

import java.util.ArrayList;
import java.util.Calendar;

import javax.ejb.Stateless;

import org.apache.log4j.Logger;

import com.phonebooksharing.dto.ReturnItem;
import com.phonebooksharing.dto.dashboard.DashboardItem;
import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.gallery.ImageGalleryItem;
import com.phonebooksharing.dto.gallery.PostCardItem;
import com.phonebooksharing.dto.gallery.VideoGalleryItem;
import com.phonebooksharing.dto.msg.TextMessageItem;
import com.phonebooksharing.dto.msg.VoiceMessageItem;

/**
 * Stateless EJB Bean with contains the facade logic which performs 
 * the desired lookups and business logic required by its method.
 * 
 * <p> Should always return an object that will be serialized by its 
 * caller and sent back to Android.
 * 
 * @author <a href='mailto:luizrafael@gmail.com'>Rafael Fernandes</a>
 *
 */
@Stateless
public class DashboardFacadeBean extends AbstractFacadeBean implements DashboardFacade {
    private static Logger log = Logger.getLogger(DashboardFacadeBean.class);

    @Override
    public DashboardItem updateDashboard(String authItem) {
        log.info("Refreshing/Updating dashboard for authItem [" + authItem + "]...");
        
        final DashboardItem di = new DashboardItem();
        di.qtyFriendsNearby = new Integer(20);
        di.qtyFriendsMessages = new Integer(20);
        di.qtyFriendsPhotos = new Integer(20);
        di.qtyFriendsVideos = new Integer(20);
        di.qtyFriendsPostcards = new Integer(20);
        
        return di;
    }

    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardMessages(String authItem, int page, int pageSize) {
        log.info("Retrieving dashboard messages for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");
        final ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
        
        ContactItem contactItem;
        DashboardReaderItem<ContactItem> dr;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            if(i % 2 == 0) {
                TextMessageItem t = new TextMessageItem();
                t.title           = "Title of Text Message nr " + i;
                t.text            = "Long text message goes here 1j1 j1jj1jj1j1jo2j ojfojflsdf ";
                t.textMessageID   = i;
                contactItem       = t;
            } else {
                VoiceMessageItem v = new VoiceMessageItem();
                v.voiceMessageID   = Long.valueOf(i);
                v.length           = "00:33";
                v.audioID          = "" + i;
                v.title            = "Title of Voice Message nr " + i;
                contactItem        = v;
            }
    
            setContactInfo(contactItem, i, true);
            contactItem.andID = 1;//must always be there once this is a dashboard from a friend
    
            dr = new DashboardReaderItem<ContactItem>(i, contactItem);
            dr.setWhen(timeInMilis);
            dashList.add(dr);
        }
        
        return dashList;
    }
    
    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardMessages(String authItem, int type, int page, int pageSize) {
        log.info("Retrieving dashboard messages for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");
        
        if(type > 0) {//should retrieve only text dashboard messages (for desktop widget)
            ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
            TextMessageItem t;
            DashboardReaderItem<ContactItem> dr;
            long timeInMilis = Calendar.getInstance().getTimeInMillis();
            for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
                t               = new TextMessageItem();
                t.title         = "Title of Text Message nr " + i;
                t.text          = "Long text message goes here 1j1 j1jj1jj1j1jo2j ojfojflsdf ";
                t.textMessageID = i;
        
                setContactInfo(t, i, true);
                t.andID = 1;//must always be there once this is a dashboard from a friend
        
                dr = new DashboardReaderItem<ContactItem>(i, t);
                dr.setWhen(timeInMilis);
                dashList.add(dr);
            }
            return dashList;
        } else {
            return getDashboardMessages(authItem, page, pageSize);
        }
    }
    
    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardImages(String authItem, int page, int pageSize) {
        log.info("Retrieving dashboard images for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");

        ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
        ImageGalleryItem fi;
        DashboardReaderItem<ContactItem> dr;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            fi = new ImageGalleryItem();
            fi.title          = "Image title is " + i;
            fi.height         = 320;
            fi.width          = 480;
            fi.photoID        = i;
            fi.imageLatitude  = "37.0625";
            fi.imageLongitude = "-95.677068";
    
            setContactInfo(fi, i, true);
            fi.andID = 1;//must always be there once this is a dashboard from a friend
    
            dr = new DashboardReaderItem<ContactItem>(i, fi);
            dr.setWhen(timeInMilis);
            dashList.add(dr);
        }
        
        return dashList;
    }

    @Override
    public ReturnItem createTextResponse(DashboardReaderItem<ContactItem> dr) {
        return new ReturnItem(true);
    }

    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardPostcards(String authItem, int page, int pageSize) {
        log.info("Retrieving dashboard postcards for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");
        
        ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
        PostCardItem fi;
        DashboardReaderItem<ContactItem> dr;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            fi = new PostCardItem();
            fi.title         = "Postcard title is " + i;
            fi.whereAt       = "Made in " + i;
            fi.postcardID    = i;
            fi.postLatitude  = "37.0625";
            fi.postLongitude = "-95.677068";
            
            setContactInfo(fi, i, true);
            fi.andID = 1;//must always be there once this is a dashboard from a friend
    
            dr = new DashboardReaderItem<ContactItem>(i, fi);
            dr.setWhen(timeInMilis);
            dashList.add(dr);
        }
        
        return dashList;
    }

    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardVideos(String authItem, int page, int pageSize) {
        log.info("Retrieving dashboard videos for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");

        ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
        VideoGalleryItem fi;
        DashboardReaderItem<ContactItem> dr;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            fi = new VideoGalleryItem();
            fi.videoLength    = "00:30";
            fi.videoID        = i;
            fi.videoLatitude  = "37.0625";
            fi.videoLongitude = "-95.677068";
            
            setContactInfo(fi, i, true);
            fi.andID = 1;//must always be there once this is a dashboard from a friend
    
            dr = new DashboardReaderItem<ContactItem>(i, fi);
            dr.setWhen(timeInMilis);
            dashList.add(dr);
        }
        
        return dashList;
    }

    @Override
    public ArrayList<DashboardReaderItem<ContactItem>> getDashboardContacts(String authItem, int page, int pageSize) {
        log.info("Retrieving dashboard contacts nearby for authItem [" + authItem + "], page [" + page + "] with pageSize [" + pageSize + "]");
        ArrayList<DashboardReaderItem<ContactItem>> dashList = new ArrayList<DashboardReaderItem<ContactItem>>();
        
        ContactItem fi;
        DashboardReaderItem<ContactItem> dr;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            fi = new ContactItem();
            
            setContactInfo(fi, i, true);
            fi.andID = 1;//must always be there once this is a dashboard from a friend
    
            dr = new DashboardReaderItem<ContactItem>(i, fi);
            dr.setWhen(timeInMilis);
            dashList.add(dr);
        }
        
        return dashList;
    }
}
